package com.atguigu.gulimall.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrRespVO;
import com.atguigu.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性相关接口
 * @author zhuanning
 * @email zhuanning@gmail.com
 * @date 2020-05-23 00:42:49
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
     private ProductAttrValueService productAttrValueService;


    /***
     * @Description: 查询规格参数分页列表
     * @author zhuanning
     * @date 2022/3/18 4:30 下午
     * @param params
     * @param catelogId
     * @return 分页数据
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId,@PathVariable("attrType")String type){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /***
     * @Description: 根据spuId查询商品属性
     * @author zhuanning
     * @date 2022/3/20 8:39 下午
     * @param spuId
     * @return
     */
    @RequestMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> list = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data", list);
    }

    /***
     * @Description: 批量更新商品关联的规格属性
     * @author zhuanning
     * @date 2022/3/20 8:42 下午
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entityList){
        productAttrValueService.updateSpuAttr(spuId,entityList);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVO attrInfo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrInfo);
    }

    /***
     * @Description:新增属性
     * @author zhuanning
     * @date 2022/3/18 9:56 下午
     * @param attr
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVO attr){
		attrService.saveAttr(attr);
        return R.ok();
    }


    /***
     * @Description: 修改属性
     * @author zhuanning
     * @date 2022/3/18 9:57 下午
     * @param attr
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attr){
		attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
